import axios from 'axios'
import ElementUI from 'element-ui';
import store from './store'
import router from './router'

axios.defaults.baseURL = 'http://192.168.1.249:8182';

//前置拦截
axios.interceptors.request.use(config => {
    return config;
});
axios.interceptors.response.use(response => {
    let res = response.data;
    if(res.access_token){
        return response
    }
    if(res.code === 200){
        return response
    }else {
        ElementUI.Message.error('请求出错!', {duration : 2000})
    }
    return Promise.reject("请求出错!");
},error => {
    if(error.response.data){
        error.message = error.response.data.error_description
    }
    if(error.response.status === 401){
        store.commit('DEL_INFO');
        router.push('/login')
    }
    ElementUI.Message.error(error.message, {duration : 2000});
    return Promise.reject("请求出错!");
});